# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from lxml import etree
import time
from fake_useragent import UserAgent
import re
import json


"""
携程-酒店
页面提取json
依据json中的id，解析html中价格、标签
https://hotels.ctrip.com/hotel/Shanghai2/p1
"""


# 初始化
def init_driver():
    chrome_options = Options()
    # 设置chrome不加载图片，提高速度
    # chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('user-agent='+UserAgent().random)  # 修改请求头，伪装浏览器型号
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 最大化

    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive, url):
    driver.get(url)
    # 设置等待
    wait = WebDriverWait(driver, 10)
    try:
        element = WebDriverWait(driver, 10).until(
                        EC.presence_of_element_located((By.XPATH, '//li[@class="set-list set-logIn"]/a')))
        element.click()
        # 用户名
        driver.find_element_by_id('nloginname').send_keys("18241255868")
        time.sleep(0.5)
        # 密码
        driver.find_element_by_id('npwd').send_keys("guo5868abc")
        time.sleep(0.5)
        # 取消30天保存
        driver.find_element_by_id('normal30day').click()
        time.sleep(0.5)
        # 提交
        driver.find_element_by_id('nsubmit').click()
        time.sleep(3)

        # 下拉滚动条至翻页(注：不执行翻页，在返回。无标签数据)
        page_height = driver.execute_script('return document.documentElement.scrollHeight')
        driver.execute_script(f"window.scrollBy(0, {page_height * 4 / 5 })")

        driver.find_element_by_xpath('//a[contains(text(),"下一页")]').click()
        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.XPATH, '//div[@class="hotel_new_list J_HotelListBaseCell"]')))
        driver.find_element_by_xpath('//a[contains(text(),"1")]').click()
        driver.execute_script(f"window.scrollBy(0, {page_height * 4 / 5 })")
        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.XPATH, '//div[@class="hotel_new_list J_HotelListBaseCell"]')))
    except:
        pass


    html = etree.HTML(driver.page_source)
    # 提取页面中的js，并解析(id、名称)
    txt_hotelPositionJSON = re.findall("window.IBU_HOTEL=(.*?)};", driver.page_source)[0] + "}"
    data_json = json.loads(txt_hotelPositionJSON)
    for d in data_json['initData']['firstPageList']['hotelList']['list']:
        dict_data = dict()
        id = d['base']['hotelId']  # id
        # dict_data['url'] = "https://hotels.ctrip.com/" + d['url']  # 链接
        dict_data['name'] = d['base']['hotelName']  # 名称
        dict_data['lat'] = d['position']['lat']  # lat
        dict_data['lon'] = d['position']['lon']  # lon
        dict_data['address'] = d['position']['address']  # 地址
        dict_data['score'] = d['score']['number']  # 总评分
        # dict_data['dpscore'] = d['dpscore']  # 用户推荐百分比
        dict_data['dpcount'] = d['comment']['content']  # 点评数量
        dict_data['star'] = d['base']['star']  # 酒店星级
        # dict_data['stardesc'] = d['stardesc']  # 酒店类型
        params_price = f'//div[contains(@class,"hotel_price") and contains(@data-id,"{id}")]/a/span/text()'
        params_tag = f'//div[contains(@class,"hotel_new_list J_HotelListBaseCell") and contains(@id,"{id}")]/ul/li[2]/p[@class="medal_list"]/span//text()'
        dict_data['price'] = html.xpath(params_price)[0]
        dict_data['tag'] = "|".join(html.xpath(params_tag))
        print(str(dict_data))

    # for x in html.xpath('//div[@class="hotel_new_list J_HotelListBaseCell"]'):
    #     title = x.xpath('./ul/li/h2/a/text()')[0]
    #     id = x.xpath('./ul/li/h2/@data-id')[0]
    #     titleUrl = x.xpath('./ul/li/h2/a/@href')[0]
    #     address = "".join(x.xpath('./ul/li/p[@class="hotel_item_htladdress"]//text()')).replace("地图", "")
    #     tag = "|".join(x.xpath('./ul/li/p[@class="medal_list"]/span//text()'))  # 标签
    #     pf = x.xpath('./ul/li[@class="hotel_item_judge no_comment "]/div[1]/a/span[2]/text()')[0]  # 评分
    #     tj = x.xpath('./ul/li[@class="hotel_item_judge no_comment "]/div[1]/a/span[3]/span/text()')[0]  # 推荐
    #     pj = x.xpath('./ul/li[@class="hotel_item_judge no_comment "]/div[1]/a/span[4]/span/text()')[0]  # 评价分数
    #     price = x.xpath('./ul/li[@class="hotel_price_icon"]/div/div/div/a/span/text()')[0]  # 价格
        # 打开新的标签页
        js = f'window.open("{dict_data["url"]}");'
        driver.execute_script(js)
        # 获取当前窗口句柄
        now_handle = driver.current_window_handle
        # 获取所有窗口句柄
        all_handles = driver.window_handles
        for handle in all_handles:
            if handle != now_handle:
                driver.switch_to.window(handle)  # 切换新打开窗口
                time.sleep(1)
                html_child = etree.HTML(driver.page_source)
                driver.close()
        # 返回主窗口
        driver.switch_to.window(now_handle)
    time.sleep(3)
    # 点击下一页
    # driver.find_element_by_xpath('//a[contains(text(),"下一页")]').click()



def close_driver(driver):
    driver.quit()


if __name__ == '__main__':

    driver = init_driver()
    for p in range(1, 10):
        url = f"https://hotels.ctrip.com/hotel/Shanghai2/p{p}"
        get_url(driver, url)
